package com.vehicle.coupon.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vehicle.coupon.mapper.CouponRecordMapper;
import com.vehicle.coupon.domain.CouponRecord;
import com.vehicle.coupon.service.ICouponRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券领取记录Service业务层处理
 *
 * @author XU
 * @date 2025-03-25
 */
@Service
public class CouponRecordServiceImpl implements ICouponRecordService {
    @Autowired
    private CouponRecordMapper couponRecordMapper;

    /**
     * 查询优惠券领取记录
     *
     * @param id 优惠券领取记录主键
     * @return 优惠券领取记录
     */
    @Override
    public CouponRecord selectCouponRecordById(Long id)
    {
        return couponRecordMapper.selectCouponRecordById(id);
    }

    /**
     * 查询优惠券领取记录列表
     *
     * @param couponRecord 优惠券领取记录
     * @return 优惠券领取记录
     */
    @Override
    public List<CouponRecord> selectCouponRecordList(CouponRecord couponRecord)
    {
        return couponRecordMapper.selectCouponRecordList(couponRecord);
    }

    /**
     * 新增优惠券领取记录
     *
     * @param couponRecord 优惠券领取记录
     * @return 结果
     */
    @Override
    public boolean checkUserReceived(Long couponId, Long userId) {
        CouponRecord query = new CouponRecord();
        query.setCouponId(couponId);
        query.setUserId(userId);
        List<CouponRecord> records = couponRecordMapper.selectCouponRecordList(query);
        return !records.isEmpty();
    }

    @Override
    @Transactional
    public int insertCouponRecord(CouponRecord couponRecord) {
        // 检查是否已领取
        if (checkUserReceived(couponRecord.getCouponId(), couponRecord.getUserId())) {
            throw new RuntimeException("您已领取过该优惠券");
        }
        
        // 执行领取操作
        return couponRecordMapper.insertCouponRecord(couponRecord);
    }

    @Override
    public int updateCouponRecord(CouponRecord couponRecord) {
        return 0;
    }

    @Override
    public int deleteCouponRecordByIds(Long[] ids) {
        return 0;
    }

    @Override
    public int deleteCouponRecordById(Long id) {
        return 0;
    }
}
